package com.soft.line.util.poi;

import com.soft.line.entity.*;
import com.soft.line.service.CertificateInfoService;
import com.soft.line.service.CodeDetailService;
import com.soft.line.service.ContractInfoService;
import com.soft.line.util.jackson.converter.StringToDoubleUtil;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PoiUtils {

    public static void certificate2Excel(List<CertificateInfo> list, HttpServletResponse response) {
        String title = "证书信息表";
        //创建一个 Excel 文档
        HSSFWorkbook workbook = new HSSFWorkbook();
        //5. 创建样式
        //创建标题行的样式
        HSSFCellStyle headerStyle = workbook.createCellStyle();
        headerStyle.setFillForegroundColor(IndexedColors.YELLOW.index);
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        HSSFSheet sheet = workbook.createSheet(title);
        //创建标题行
        HSSFRow r0 = sheet.createRow(0);
        HSSFCell c0 = r0.createCell(0);
        c0.setCellValue("证书名称");
        c0.setCellStyle(headerStyle);
        HSSFCell c1 = r0.createCell(1);
        c1.setCellStyle(headerStyle);
        c1.setCellValue("证书编号");
        HSSFCell c2 = r0.createCell(2);
        c2.setCellStyle(headerStyle);
        c2.setCellValue("证书类型");
        HSSFCell c3 = r0.createCell(3);
        c3.setCellStyle(headerStyle);
        c3.setCellValue("颁发单位");
        HSSFCell c4 = r0.createCell(4);
        c4.setCellStyle(headerStyle);
        c4.setCellValue("颁发日期");
        HSSFCell c5 = r0.createCell(5);
        c5.setCellStyle(headerStyle);
        c5.setCellValue("起始日期");
        HSSFCell c6 = r0.createCell(6);
        c6.setCellStyle(headerStyle);
        c6.setCellValue("终止日期");
        HSSFCell c7 = r0.createCell(7);
        c7.setCellStyle(headerStyle);
        c7.setCellValue("备注");
        for (int i = 0; i < list.size(); i++) {
            CertificateInfo cert = list.get(i);
            HSSFRow row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(cert.getCertName());
            row.createCell(1).setCellValue(cert.getCertId() == null ? "" : cert.getCertId());
            row.createCell(2).setCellValue(cert.getCertType());
            row.createCell(3).setCellValue(cert.getCertUnit());
            row.createCell(4).setCellValue(dateToString(cert.getIssueDate()));
            row.createCell(5).setCellValue(dateToString(cert.getBeginDate()));
            row.createCell(6).setCellValue(dateToString(cert.getEndDate()));
            row.createCell(7).setCellValue(cert.getNote() == null ? "" : cert.getNote());
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            workbook.write(baos);
            response.setContentType("application/octet-stream;charset=utf-8");
            OutputStream outputStream = response.getOutputStream();
            outputStream.write(baos.toByteArray());
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void contract2Excel(List<ContractInfo> list, HttpServletResponse response) {
        String title = "合同信息表";
        //创建一个 Excel 文档
        HSSFWorkbook workbook = new HSSFWorkbook();
        //5. 创建样式
        //创建标题行的样式
        HSSFCellStyle headerStyle = workbook.createCellStyle();
        headerStyle.setFillForegroundColor(IndexedColors.YELLOW.index);
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        HSSFSheet sheet = workbook.createSheet(title);
        //创建标题行
        HSSFRow r0 = sheet.createRow(0);
        HSSFCell c0 = r0.createCell(0);
        c0.setCellValue("部门");
        c0.setCellStyle(headerStyle);
        HSSFCell c1 = r0.createCell(1);
        c1.setCellStyle(headerStyle);
        c1.setCellValue("合同名称");
        HSSFCell c2 = r0.createCell(2);
        c2.setCellStyle(headerStyle);
        c2.setCellValue("合同类别");
        HSSFCell c3 = r0.createCell(3);
        c3.setCellStyle(headerStyle);
        c3.setCellValue("项目编号");
        HSSFCell c4 = r0.createCell(4);
        c4.setCellStyle(headerStyle);
        c4.setCellValue("总金额");
        HSSFCell c5 = r0.createCell(5);
        c5.setCellStyle(headerStyle);
        c5.setCellValue("甲方");
        HSSFCell c6 = r0.createCell(6);
        c6.setCellStyle(headerStyle);
        c6.setCellValue("乙方");
        HSSFCell c7 = r0.createCell(7);
        c7.setCellStyle(headerStyle);
        c7.setCellValue("其他签约方");
        HSSFCell c8 = r0.createCell(8);
        c8.setCellStyle(headerStyle);
        c8.setCellValue("签约日期");
        HSSFCell c9 = r0.createCell(9);
        c9.setCellStyle(headerStyle);
        c9.setCellValue("签约地点");
        HSSFCell c10 = r0.createCell(10);
        c10.setCellStyle(headerStyle);
        c10.setCellValue("起始日期");
        HSSFCell c11 = r0.createCell(11);
        c11.setCellStyle(headerStyle);
        c11.setCellValue("终止日期");
        HSSFCell c12 = r0.createCell(12);
        c12.setCellStyle(headerStyle);
        c12.setCellValue("付款方式");
        HSSFCell c13 = r0.createCell(13);
        c13.setCellStyle(headerStyle);
        c13.setCellValue("备注");
        HSSFCell c14 = r0.createCell(14);
        c14.setCellStyle(headerStyle);
        c14.setCellValue("合同性质");
        HSSFCell c15 = r0.createCell(15);
        c15.setCellStyle(headerStyle);
        c15.setCellValue("合同内容");
        for (int i = 0; i < list.size(); i++) {
            ContractInfo cont = list.get(i);
            HSSFRow row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(cont.getDepId());
            row.createCell(1).setCellValue(cont.getContractName());
            row.createCell(2).setCellValue(cont.getContractType());
            row.createCell(3).setCellValue(cont.getProId() == null ? "" : cont.getProId());
            row.createCell(4).setCellValue(cont.getTotalAmount().toString());
            row.createCell(5).setCellValue(cont.getPartA());
            row.createCell(6).setCellValue(cont.getPartB());
            row.createCell(7).setCellValue(cont.getPartC() == null ? "" : cont.getPartC());
            row.createCell(8).setCellValue(dateToString(cont.getSignTime()));
            row.createCell(9).setCellValue(cont.getSignAddr());
            row.createCell(10).setCellValue(dateToString(cont.getBeginDate()));
            row.createCell(11).setCellValue(dateToString(cont.getEndDate()));
            row.createCell(12).setCellValue(cont.getPayment());
            row.createCell(13).setCellValue(cont.getNote() == null ? "" : cont.getNote());
            row.createCell(14).setCellValue(cont.getContractNature() == null ? "" : cont.getContractNature());
            row.createCell(15).setCellValue(cont.getContractContent() == null ? "" : cont.getContractContent());
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            workbook.write(baos);
            response.setContentType("application/octet-stream;charset=utf-8");
            OutputStream outputStream = response.getOutputStream();
            outputStream.write(baos.toByteArray());
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static Map<String, Object> excel2Certificate(MultipartFile file, CodeDetailService codeDetailService, CertificateInfoService certificateInfoService) {
        List<CertificateInfo> certificateInfoList = new ArrayList<CertificateInfo>();
        CertificateInfo cert = null;
        Map<String, Object> map = new HashMap<String, Object>();
        String warningMsg = "";
        String chgrow = "\r\n";
        try {
            String fileName = file.getOriginalFilename();
            boolean isExcel2003 = true;
            if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
                isExcel2003 = false;
            }
            InputStream is = file.getInputStream();
            //1. 创建一个 workbook 对象
            Workbook workbook = null;
            if (isExcel2003) {
                workbook = new HSSFWorkbook(is);
            } else {
                workbook = new XSSFWorkbook(is);
            }
            //2. 获取 workbook 中表单的数量
            int numberOfSheets = workbook.getNumberOfSheets();
            for (int i = 0; i < numberOfSheets; i++) {
                //3. 获取表单
                Sheet sheet = workbook.getSheetAt(i);
                //4. 获取表单中的行数
                int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();

                for (int j = 0; j < physicalNumberOfRows; j++) {
                    //5. 跳过标题行
                    if (j == 0) {
                        if (sheet.getRow(j).getLastCellNum() != 8) {
                            warningMsg += "证书导入模板格式不对！" + chgrow;
                            break;
                        } else {
                            continue;//跳过标题行
                        }
                    }
                    int rowCount = j + 1;
                    //6. 获取行
                    Row row = sheet.getRow(j);
                    if (row == null) {
                        continue;//防止数据中间有空行
                    }
                    //7. 获取列数
                    //int physicalNumberOfCells = row.getPhysicalNumberOfCells();
                    int physicalNumberOfCells = row.getLastCellNum();
                    cert = new CertificateInfo();
                    for (int k = 0; k < physicalNumberOfCells; k++) {
                        Cell cell = row.getCell(k);
                        String cellValue = null;
                        if (cell != null) {
                            cell.setCellType(CellType.STRING);
                            cellValue = cell.getStringCellValue();
                        }
                        LasCodeItem lasCodeItem = null;
                        switch (k) {
                            case 0:
                                //证书名称
                                if (cellValue == null || "".equals(cellValue)) {
                                    warningMsg += "第" + rowCount + "行证书名称不存在 " + chgrow;
                                } else if (cellValue.length() > 100) {
                                    warningMsg += "第" + rowCount + "行证书名称应不大于100字 " + chgrow;
                                } else {
                                    cert.setCertName(cellValue);
                                }
                                break;
                            case 1:
                                //证书编号
                                if (cellValue != null && !"".equals(cellValue) && certificateInfoService.countByCId(null, cellValue) > 0) {
                                    warningMsg += "第" + rowCount + "行存在相同的证书编号 " + chgrow;
                                } else {
                                    cert.setCertId(cellValue);
                                }
                                break;
                            case 2:
                                //证书类型
                                lasCodeItem = codeDetailService.selectByItemDesc("103", cellValue);
                                if (lasCodeItem == null) {
                                    warningMsg += "第" + rowCount + "行证书类型不存在 " + chgrow;
                                } else {
                                    cert.setCertType(lasCodeItem.getItemVal());
                                }
                                break;
                            case 3:
                                //颁发单位
                                if (cellValue == null || "".equals(cellValue)) {
                                    warningMsg += "第" + rowCount + "行发奖单位为空 " + chgrow;
                                } else {
                                    cert.setCertUnit(cellValue);
                                }
                                break;
                            case 4:
                                //颁发日期
                                if (cellValue != null && !"".equals(cellValue) && !isDate(cellValue)) {
                                    warningMsg += "第" + rowCount + "行颁发日期格式错误!日期样式：【20100101】" + chgrow;
                                } else {
                                    cert.setIssueDate(StringToDate(cellValue));
                                }
                                break;
                            case 5:
                                //发证日期
                                if (cellValue != null && !"".equals(cellValue) && !isDate(cellValue)) {
                                    warningMsg += "第" + rowCount + "行发证日期格式错误!日期样式：【20100101】" + chgrow;
                                } else {
                                    cert.setBeginDate(StringToDate(cellValue));
                                }
                                break;
                            case 6:
                                //到期日期
                                if (cellValue != null && !"".equals(cellValue) && !isDate(cellValue)) {
                                    warningMsg += "第" + rowCount + "行到期日期格式错误!日期样式：【20100101】" + chgrow;
                                } else if (cert.getBeginDate() != null && cert.getEndDate() != null) {
                                    if (cert.getBeginDate().after(cert.getEndDate())) {
                                        warningMsg += "第" + rowCount + "行发证日期大于到期日期 " + chgrow;
                                    }
                                } else {
                                    cert.setEndDate(StringToDate(cellValue));
                                }
                                break;
                            case 7:
                                //备注
                                cert.setNote(cellValue);
                                break;

                        }
                    }
                    certificateInfoList.add(cert);
                }
            }
            map.put("warningMsg", warningMsg);
            map.put("certificateInfoList", certificateInfoList);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            return map;
        }
    }

    public static Map<String, Object> excel2ContractInfo(MultipartFile file, CodeDetailService codeDetailService, ContractInfoService contractInfoService) {
        Map<String, Object> map = new HashMap<String, Object>();
        String warningMsg = "";
        String chgrow = "\r\n";
        List<ContractInfo> contractInfoList = new ArrayList<ContractInfo>();
        ContractInfo cont = null;
        try {
            String fileName = file.getOriginalFilename();
            boolean isExcel2003 = true;
            if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
                isExcel2003 = false;
            }
            InputStream is = file.getInputStream();
            //1. 创建一个 workbook 对象
            Workbook workbook = null;
            if (isExcel2003) {
                workbook = new HSSFWorkbook(is);
            } else {
                workbook = new XSSFWorkbook(is);
            }
            //2. 获取 workbook 中表单的数量
            int numberOfSheets = workbook.getNumberOfSheets();
            for (int i = 0; i < numberOfSheets; i++) {
                //3. 获取表单
                Sheet sheet = workbook.getSheetAt(i);
                //4. 获取表单中的行数
                int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
                for (int j = 0; j < physicalNumberOfRows; j++) {
                    //5. 跳过标题行
                    if (j == 0) {
                        if (sheet.getRow(j).getLastCellNum() != 16) {
                            warningMsg += "合同导入模板格式不对！" + chgrow;
                            break;
                        } else {
                            continue;//跳过标题行
                        }
                    }
                    int rowCount = j + 1;
                    //6. 获取行
                    Row row = sheet.getRow(j);
                    if (row == null) {
                        continue;//防止数据中间有空行
                    }
                    //7. 获取列数
                    //int physicalNumberOfCells = row.getPhysicalNumberOfCells();
                    int physicalNumberOfCells = row.getLastCellNum();
                    cont = new ContractInfo();
                    for (int k = 0; k < physicalNumberOfCells; k++) {
                        Cell cell = row.getCell(k);
                        //System.out.println("k:" + k+" j:"+j);
                        String cellValue = null;
                        if (cell != null) {
                            cell.setCellType(CellType.STRING);
                            cellValue = cell.getStringCellValue();
                        }
                        LasCodeItem lasCodeItem = null;
                        switch (k) {
                            case 0:
                                //部门
                                if (cellValue == null || "".equals(cellValue)) {
                                    warningMsg += "第" + rowCount + "行部门名称为空 " + chgrow;
                                } else {
                                    lasCodeItem = codeDetailService.selectByItemDesc("101", cellValue);
                                    if (lasCodeItem == null) {
                                        cont.setDepId(codeDetailService.selectByItemDesc("101", "其他").getItemVal());
                                    } else {
                                        cont.setDepId(lasCodeItem.getItemVal());
                                    }
                                }
                                break;
                            case 1:
                                //合同名称
                                if (cellValue == null || "".equals(cellValue)) {
                                    warningMsg += "第" + rowCount + "行合同名称为空 " + chgrow;
                                } else if (contractInfoService.countContractByIdAndName(null, cellValue) > 0) {
                                    warningMsg += "第" + rowCount + "行存在相同的合同名称 " + chgrow;
                                } else if (cellValue.length() > 100) {
                                    warningMsg += "第" + rowCount + "行合同名称应不大于100字 " + chgrow;
                                } else {
                                    cont.setContractName(cellValue);
                                }
                                break;
                            case 2:
                                //合同类别
                                lasCodeItem = codeDetailService.selectByItemDesc("102", cellValue);
                                if (lasCodeItem == null) {
                                    warningMsg += "第" + rowCount + "行合同类别不存在 " + chgrow;
                                } else {
                                    cont.setContractType(lasCodeItem.getItemVal());
                                }
                                break;
                            case 3:
                                //项目编号
                                cont.setProId(cellValue);
                                break;
                            case 4:
                                //总金额
                                if (cellValue == null || "".equals(cellValue)) {
                                    warningMsg += "第" + rowCount + "行总金额为空 " + chgrow;
                                } else if (!isNumber(cellValue)) {
                                    warningMsg += "第" + rowCount + "行总金额不是数字 " + chgrow;
                                } else {
                                    cont.setTotalAmount(new BigDecimal(StringToDoubleUtil.convert(cellValue)));
                                }
                                break;
                            case 5:
                                //甲方
                                if (cellValue == null || "".equals(cellValue)) {
                                    warningMsg += "第" + rowCount + "行甲方为空 " + chgrow;
                                } else if (cellValue.length() > 100) {
                                    warningMsg += "第" + rowCount + "行甲方名称应不大于100字 " + chgrow;
                                } else {
                                    cont.setPartA(cellValue);
                                }
                                break;
                            case 6:
                                //乙方
                                if (cellValue == null || "".equals(cellValue)) {
                                    warningMsg += "第" + rowCount + "行乙方为空 " + chgrow;
                                } else if (cellValue.length() > 100) {
                                    warningMsg += "第" + rowCount + "行合乙方称应不大于100字 " + chgrow;
                                } else {
                                    cont.setPartB(cellValue);
                                }
                                break;
                            case 7:
                                //其他签约方
                                if (cellValue != null && !"".equals(cellValue) && cellValue.length() > 100) {
                                    warningMsg += "第" + rowCount + "行其他签约方名称应不大于100字 " + chgrow;
                                } else {
                                    cont.setPartC(cellValue);
                                }
                                break;
                            case 8:
                                //签约日期
                                if (!isDate(cellValue)) {
                                    warningMsg += "第" + rowCount + "行签约日期格式错误!日期样式：【20100101】" + chgrow;
                                } else {
                                    cont.setSignTime(StringToDate(cellValue));
                                }
                                break;
                            case 9:
                                //签约地点
                                if (cellValue == null || "".equals(cellValue)) {
                                    warningMsg += "第" + rowCount + "行签约地点为空" + chgrow;
                                } else {
                                    cont.setSignAddr(cellValue);
                                }
                                break;
                            case 10:
                                //起始日期
                                if (!isDate(cellValue)) {
                                    warningMsg += "第" + rowCount + "行起始日期格式错误!日期样式：【20100101】" + chgrow;
                                } else {
                                    cont.setBeginDate(StringToDate(cellValue));
                                }
                                break;
                            case 11:
                                //终止日期
                                if (!isDate(cellValue)) {
                                    warningMsg += "第" + rowCount + "行终止日期格式错误!日期样式：【20100101】" + chgrow;
                                } else {
                                    cont.setEndDate(StringToDate(cellValue));
                                }
                                if (cont.getBeginDate() != null && cont.getEndDate() != null) {
                                    if (cont.getBeginDate().after(cont.getEndDate())) {
                                        warningMsg += "第" + rowCount + "行起始日期大于终止日期 " + chgrow;
                                    }
                                }
                                break;

                            case 12:
                                //付款方式
                                if (cellValue == null || "".equals(cellValue)) {
                                    warningMsg += "第" + rowCount + "行付款方式为空 " + chgrow;
                                } else if (cellValue.length() > 500) {
                                    warningMsg += "第" + rowCount + "行付款方式应不大于500字 " + chgrow;
                                } else {
                                    cont.setPayment(cellValue);
                                }
                                break;
                            case 13:
                                //备注
                                if (cellValue != null && !"".equals(cellValue) && cellValue.length() > 1000) {
                                    warningMsg += "第" + rowCount + "行备注应不大于1000字 " + chgrow;
                                } else {
                                    cont.setNote(cellValue);
                                }
                                break;
                            case 14:
                                //合同性质
                                lasCodeItem = codeDetailService.selectByItemDesc("104", cellValue);
                                if (lasCodeItem == null) {
                                    warningMsg += "第" + rowCount + "行合同性质不存在 " + chgrow;
                                } else {
                                    cont.setContractNature(lasCodeItem.getItemVal());
                                }
                                break;
                            case 15:
                                //合同内容
                                if (cellValue != null && !"".equals(cellValue) && cellValue.length() > 1000) {
                                    warningMsg += "第" + rowCount + "行合同内容应不大于1000字 " + chgrow;
                                } else {
                                    cont.setContractContent(cellValue);
                                }
                                break;
                        }
                    }
                    contractInfoList.add(cont);
                }
            }
            map.put("warningMsg", warningMsg);
            map.put("contractInfoList", contractInfoList);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return map;
        }
    }

    public static String dateToString(Date date) {
        if (date != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            return sdf.format(date);
        } else {
            return "";
        }
    }

    /**
     * 判断字符串是否是数字
     */
    public static boolean isNumber(String value) {
        return isInteger(value) || isDouble(value) || isIntegerDouble(value);
    }

    /**
     * 判断字符串是否是整数
     */
    public static boolean isInteger(String value) {
        try {
            Integer.parseInt(value);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    /**
     * 判断字符串是否是浮点数
     */
    public static boolean isDouble(String value) {
        try {
            Double.parseDouble(value);
            if (value.contains(".")) {
                return true;
            }
            return false;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    /**
     * 判断字符串是否是浮点数(不包含小数点)
     */
    public static boolean isIntegerDouble(String value) {
        try {
            Double.parseDouble(value);
            if (value.contains(".")) {
                return false;
            }
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    //判断是否日期。格式yyyymmdd，基本上把闰年和2月等的情况都考虑进去了
    public static boolean isDate(String date) {
//       String s = "^((((1[6-9]|[2-9]\\d)\\d{2})(0?[13578]|1[02])(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})(0?[13456789]|1[012])(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})0?2(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))0?229))$";
        String s = "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))0229)";
        Pattern p = Pattern.compile(s);
        Matcher m = p.matcher(date);
        if (m.matches()) {
            return true;
        } else {
            return false;
        }
    }

    public static Date StringToDate(String source) {
        Date date = null;
        boolean flag = false;
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        try {
            if (source == null || source.length() == 0) {
                return date;
            } else {
                source = source.trim();
                date = dateFormat.parse(source);
                flag = true;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        if (flag) {
            return date;
        } else {
            throw new IllegalArgumentException("不能解析日期:" + source);
        }

    }

}
